/** 
* 留言action类
* 
* @author  周郎(zhoulang),web@zhoulang.net
* @link http://www.zhoulang.net
* @version syuebook v1.0
* @Copyright 2009 syue.com, Inc. All rights reserved.
* @since   JDK1.6
*/  

package net.zhoulang.action;

import java.util.Date;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.apache.log4j.Logger;
import net.zhoulang.entity.BookEntity;
import net.zhoulang.service.BookService;
import net.zhoulang.utility.SitePage;
import net.zhoulang.utility.AdminUser;

public class BookAction extends ActionSupport {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	//提示信息
	private String message;
	//log4j声明
	static Logger log = Logger.getLogger(BookAction.class.getName());
	
	private BookEntity bookEntity;
    private BookService bookService;

    public void setBookEntity(BookEntity bookEntity) {
        this.bookEntity = bookEntity;
    }
    public BookEntity getBookEntity() {
        return this.bookEntity;
    }

    public void setBookService(BookService bookService) {
        this.bookService = bookService;
    }
    public BookService getBookService() {
        return this.bookService;
    }
    /**
     * 添加留言
     * @return String
     */
    public String insert() {
        try {
            this.bookEntity.setBooktime(new Date());
			this.bookEntity.setBookip(getUserIP());
            int bookId = this.bookService.bookAdd(this.bookEntity);
            setAttribute("BookId", bookId);
            setMessage("添加成功！");
            return SUCCESS;
        }
        catch(Exception e) {
            log.error(e.toString());
            setMessage("添加失败！");
        }
        return INPUT;
    }
    /**
     * 删除留言
     * @return String
     */
    public String delete() {
    	if(getSession("admin") == null) {
    		setMessage("请登录！");
    		return SUCCESS;
    	}
        try {
        	int id = getParameterInt("id");
            this.bookService.bookDel(id);
            setMessage("删除成功！");
        }
        catch(Exception e) {
            log.error(e.toString());
            setMessage("删除失败！");
        }
        return SUCCESS;
    }
    /**
     * 获取留言列表
     * @return String
     */
    public String bookList() {
    	try {
    		int page = getParameterInt("page");
    		int size = 10;
    		List<BookEntity> list = this.bookService.getBookList(page, size);
    		setAttribute("bookList", list);
    		SitePage sitePage = new SitePage();
    		sitePage.setCurrentPage(page);
    		sitePage.setPageSize(size);
    		sitePage.setPageUrl("default.zhoulang");
    		sitePage.setRecordCount(this.bookService.getBookCount());
    		setAttribute("bookPage", sitePage.getPage());
    	}
    	catch(Exception e) {
    		log.error(e.toString());
    	}
    	return SUCCESS;
    }
    /**
     * 用户登录
     * @return
     */
    public String login() {
    	try {
    		String username = getParameter("username");
    		String userpwd = getParameter("userpwd");
    		AdminUser user= new AdminUser();
    		if(user.getLogin(username, userpwd)) {
    			setSession("admin", username);
    			setMessage("登录成功！");
                return SUCCESS;
    		}
    		else {
    			setMessage("登录失败！");
    		}
    	}
    	catch(Exception e) {
    		log.error(e.toString());
    		setMessage("登录失败！");
        }
        return INPUT;
    }
    /**
     * 用于处理url问题
     * @return String
     */
    public String successPage() {
    	return SUCCESS;
    }
    /**
     * 获取用户IP
     * @return String
     */
	private String getUserIP() {
		return ServletActionContext.getRequest().getRemoteHost();
	}
	/**
	 * 读取表单参数
	 * @param name
	 * @return
	 */
	private String getParameter(String name) {
		return ServletActionContext.getRequest().getParameter(name);
	}
	/**
	 * 将表单参数作为整数返回
	 * @param name
	 * @return
	 */
    private int getParameterInt(String name) {
		String par = getParameter(name);
		if(par == null) {
			return 1;
		}
		else {
			try {
				return Integer.parseInt(par);
			}
			catch(NumberFormatException e) {
				e.printStackTrace();
			}
		}
		return 1;
	}
    /**
     * 设置request的属性
     * @param name
     * @param value
     */
	public void setAttribute(String name, Object value) {
        ServletActionContext.getRequest().setAttribute(name, value);
    }
	/**
	 * 读取request的属性
	 * @param name
	 * @return
	 */
    public Object getAttribute(String name) {
        return ServletActionContext.getRequest().getAttribute(name);
    }
    /**
     * 获取请求对象
     * @return
     */
    public static HttpServletRequest getRequest() {
        return ServletActionContext.getRequest();
    }
    /**
     * 设置session值
     * @param name
     * @param value
     */
    @SuppressWarnings("unchecked")
	public static void setSession(String name, Object value) {
    	ActionContext ctx = ActionContext.getContext();
    	Map session = ctx.getSession();
    	session.put(name, value);
    }
    /**
     * 获取session值
     * @param name
     * @return
     */
    @SuppressWarnings("unchecked")
	public static Object getSession(String name) {
    	ActionContext ctx = ActionContext.getContext();
    	Map session = ctx.getSession();
    	return session.get(name);
    }
    /**
     * 设置返回信息
     * @param message
     */
    public void setMessage(String message) {
    	this.message = message;
    }
    /**
     * 获取返回信息
     * @return
     */
    public String getMessage() {
    	return this.message;
    }
}
